cmake_minimum_required (VERSION 3.8)
project(LibDGGSFieldPyramid)
set(CMAKE_CXX_STANDARD 17)

if (MSVC)
    add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
endif ()

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    set(DGGS_OUT_DIR ${CMAKE_OUT_DIR}/out/build/debug)
else ()
    set(DGGS_OUT_DIR ${CMAKE_OUT_DIR}/out/build/release)
endif ()

include_directories("${CMAKE_SOURCE_DIR}/LibDGGSBase")

include_directories("${CMAKE_SOURCE_DIR}/LibDGGSKernel")
link_directories("${DGGS_OUT_DIR}/LibDGGSKernel")

include_directories("${CMAKE_SOURCE_DIR}/LibDGGSPyramidKernel")
link_directories("${DGGS_OUT_DIR}/LibDGGSPyramidKernel")

include_directories("${CMAKE_SOURCE_DIR}/3rdlibs/include")
link_directories("${CMAKE_SOURCE_DIR}/3rdlibs/lib")

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibDGGSFieldPyramid)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibDGGSFieldPyramid)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${DGGS_OUT_DIR}/LibDGGSFieldPyramid)

set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)

set(CMAKE_DEBUG_POSTFIX D)
set(CMAKE_RELEASE_POSTFIX )

file(GLOB_RECURSE SOURCE_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
add_library(LibDGGSFieldPyramid SHARED ${SOURCE_FILES}
        DGGSFieldPyramidImpl.hpp
        DGGSFieldPyramidImpl.cpp
        IFieldPyramidKernel.hpp
        LibFieldPyramidKernel.cpp
)

add_dependencies(LibDGGSFieldPyramid LibDGGSKernel)

if(ISEXTRACT)
    file(GLOB_RECURSE MyLIB_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.hpp)
    foreach(header_file ${MyLIB_HEADERS})
        get_filename_component(header_dir ${header_file} DIRECTORY)
        string(REPLACE ${CMAKE_CURRENT_SOURCE_DIR} ${lib_output_directory}/include/LibDGGSFieldPyramid output_header_dir ${header_dir})
        file(MAKE_DIRECTORY ${output_header_dir})
        configure_file(${header_file} ${output_header_dir} COPYONLY)
    endforeach()
    if (CMAKE_BUILD_TYPE STREQUAL "Debug")
        add_custom_command(TARGET LibDGGSFieldPyramid POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/LibDGGSFieldPyramidD.lib
                ${lib_output_directory}/lib/debug/LibDGGSFieldPyramidD.lib
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/LibDGGSFieldPyramidD.dll
                ${lib_output_directory}/bin/debug/LibDGGSFieldPyramidD.dll
                COMMENT "Copying LibDGGSFieldPyramid debug files..."
        )
    else ()
        add_custom_command(TARGET LibDGGSFieldPyramid POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/LibDGGSFieldPyramid.lib
                ${lib_output_directory}/lib/release/LibDGGSFieldPyramid.lib
                COMMAND ${CMAKE_COMMAND} -E copy
                ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/LibDGGSFieldPyramid.dll
                ${lib_output_directory}/bin/release/LibDGGSFieldPyramid.dll
                COMMENT "Copying LibDGGSFieldPyramid release files..."
        )
    endif ()
endif()

# 安装库和头文件
install(TARGETS LibDGGSFieldPyramid
        LIBRARY DESTINATION ${lib_install_directory}/lib
        ARCHIVE DESTINATION ${lib_install_directory}/lib
        RUNTIME DESTINATION ${lib_install_directory}/bin
        PUBLIC_HEADER DESTINATION ${lib_install_directory}/include
)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/" # source directory
        DESTINATION ${lib_install_directory}/include # target directory
        FILES_MATCHING # install only matched files
        PATTERN "*.hpp" # select header files
)

if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    target_link_libraries(LibDGGSFieldPyramid
            LibDGGSKernelD
            jsoncpp
    )
else ()
    target_link_libraries(LibDGGSFieldPyramid
            LibDGGSKernel
            jsoncpp
    )
endif ()
